/**
 * 
 */
package net.xfok.mail.sender;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import android.util.Log;

/**
 * @author user
 *
 */
public class JMailSender extends MailSender {
	private static final String TAG="JMailSender";
	
	public JMailSender(String smtp,String username,String password){
		super(smtp, username, password);
	}
	
	public void send(String from, String to, String subject, String body) {
		try {
			Properties props = new Properties();
			props.put("mail.smtp.host", smtp);
			props.put("mail.smtp.auth", "true");
			Session ssn = Session.getInstance(props, null);
			MimeMessage message = new MimeMessage(ssn);
			InternetAddress fromAddress = new InternetAddress(from);
			message.setFrom(fromAddress);
			message.addRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false));
			message.setSubject(subject);
			message.setText(body);

			Transport transport = ssn.getTransport("smtp");
			transport.connect(smtp, username, password);
			transport.sendMessage(message, message
					.getRecipients(Message.RecipientType.TO));
			transport.close();
			Log.i(TAG, "Send sucess!");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void send(String to, String subject, String body) {
		send(username, to, subject, body);
	}
}
